package com.example.springboot.batch.controller;

import org.springframework.batch.core.*;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @desc
 * @Author wangsh
 * @date 2018/5/14 21:42
 */
@RestController
public class BatchController {

	@Autowired
	JobLauncher jobLauncher;

	@Autowired
	Job importJob;
	public JobParameters jobParameters;

	@RequestMapping("/importExcel")
	public String importExcel(String fileName) throws Exception {

		String path = fileName + ".csv";
		jobParameters = new JobParametersBuilder()
				.addLong("time", System.currentTimeMillis())
				.addString("input.file.name", path)
				.toJobParameters();
		JobExecution run = jobLauncher.run(importJob, jobParameters);

		BatchStatus status = run.getStatus();
		System.out.println(status);

		return "ok";
	}
}
